package entity;

import lombok.Data;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;


/**
 * Created by Alan on 17-9-28
 */
@Data
public class UserEntity implements WritableComparable<UserEntity> {
    private Integer id;
    private String mobile;
    private String name;
    private Double monthIncome;
    private String address;
    private Double yearIncome;

    public UserEntity() {
    }

    public void set(Integer id, String mobile, String name, Double monthIncome, String address, Double yearIncome){
        this.id = id;
        this.mobile = mobile;
        this.name = name;
        this.monthIncome = monthIncome;
        this.address = address;
        this.yearIncome = yearIncome;
    }


    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(id);
        dataOutput.writeUTF(mobile);
        dataOutput.writeUTF(name);
        dataOutput.writeDouble(monthIncome);
        dataOutput.writeUTF(address);
        dataOutput.writeDouble(yearIncome);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        id = dataInput.readInt();
        mobile = dataInput.readUTF();
        name = dataInput.readUTF();
        monthIncome = dataInput.readDouble();
        address = dataInput.readUTF();
        yearIncome = dataInput.readDouble();
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "id=" + id +
                ", mobile='" + mobile + '\'' +
                ", name='" + name + '\'' +
                ", monthIncome=" + monthIncome +
                ", address='" + address + '\'' +
                ", yearIncome=" + yearIncome +
                '}';
    }

    @Override
    public int compareTo(UserEntity o) {
        if(this.id == o.getId()){
            return 0;
        }else{
            return this.id - o.getId() > 0 ? 1 : -1;
        }
    }
}
